package com.zbkj.service.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zbkj.common.model.merchant.MerchantApply;
import com.zbkj.common.model.merchant.MerchantIncomeExpenditure;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.MerchantIncomeExpenditureRequest;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.response.MerchantIncomeExpenditureResponse;
import com.zbkj.service.dao.MerchantIncomeExpenditureDao;
import com.zbkj.service.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

//MerchantIncomeExpenditureServiceImpl 接口实现
@Service
public class MerchantIncomeExpenditureServiceImpl extends ServiceImpl<MerchantIncomeExpenditureDao, MerchantIncomeExpenditure> implements MerchantIncomeExpenditureService {

    @Resource
    private MerchantIncomeExpenditureDao dao;

    private final Logger logger = LoggerFactory.getLogger(MerchantIncomeExpenditureServiceImpl.class);

    @Override
    public PageInfo<MerchantIncomeExpenditure> getList(MerchantIncomeExpenditureRequest expenditureRequest, PageParamRequest pageParamRequest) {
        Page<MerchantApply> page = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        LambdaQueryWrapper<MerchantIncomeExpenditure> lqw = Wrappers.lambdaQuery();
        lqw.eq(MerchantIncomeExpenditure::getMerId, expenditureRequest.getMerId());
        lqw.eq(MerchantIncomeExpenditure::getType, expenditureRequest.getType());
        lqw.eq(MerchantIncomeExpenditure::getIsDel, 0);
        lqw.eq(StringUtils.isNotBlank(expenditureRequest.getStatus()), MerchantIncomeExpenditure::getMerId, expenditureRequest.getStatus());
        if (Objects.nonNull(expenditureRequest.getCreateTime()) && Objects.nonNull(expenditureRequest.getEndTime()))
            lqw.between(MerchantIncomeExpenditure::getCreateTime, expenditureRequest.getCreateTime(), expenditureRequest.getEndTime());

        lqw.orderByDesc(MerchantIncomeExpenditure::getCreateTime, MerchantIncomeExpenditure::getId);
        List<MerchantIncomeExpenditure> expenditureList = dao.selectList(lqw);
        return CommonPage.copyPageInfo(page, expenditureList);
    }

    @Override
    public MerchantIncomeExpenditureResponse getById(Integer id) {
        MerchantIncomeExpenditure merchantIncomeExpenditure = dao.selectById(id);
        MerchantIncomeExpenditureResponse response = new MerchantIncomeExpenditureResponse();
        BeanUtils.copyProperties(merchantIncomeExpenditure, response);
        return response;
    }

    @Override
    public Boolean create(MerchantIncomeExpenditure incomeExpenditure) {
        return dao.insert(incomeExpenditure)>0 ? true:false;
    }

    @Override
    public Boolean updateIncomeExpenditure(MerchantIncomeExpenditure incomeExpenditure) {
        return dao.updateById(incomeExpenditure)>0 ? true:false;
    }

    @Override
    public Boolean deleteById(Integer id) {
        return dao.deleteById(id)>0 ? true:false;
    }
}

